/**
 * Copyright 2020 Inc.
 **/
package com.myz.cloud.stream.rocketmq.consumer.stream;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author maoyz0621 on 2020/8/24
 * @version v1.0
 */
@Component
@Slf4j
public class MessageSender<T> {

    @Resource
    StreamOutputChannel streamOutputChannel;

    @Autowired
    @Qualifier(StreamOutputChannel.PUSH)
    MessageChannel messageChannel;

    public void sendMessage(T message) {
        streamOutputChannel.output().send(MessageBuilder.withPayload(message).build());
    }

    public void push(T message) {
        messageChannel.send(MessageBuilder.withPayload(message).build());
    }

    public void bind(T message) {
        streamOutputChannel.bind().send(MessageBuilder.withPayload(message).setHeader("type", "bar").build());
    }

    // Cannot find a @StreamListener matching for message with id:
    public void bindNoHeader(T message) {
        streamOutputChannel.bind().send(MessageBuilder.withPayload(message).setHeader("type", "bar1").build());
    }
}